import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { Ref, useRef } from 'react';
import { VersionManageAPI } from '../common/type';
import { message } from 'antd';
import MyIcon from '@/components/MyIcon';
import { addVersionService, directlyIssueService } from '../services';
import { history } from 'umi';
import { getKeyRouter } from '@/utils';
export const DirectlyIssueModal: React.FC<VersionManageAPI.IVersionDirectlyIssueModal> = ({
  onClose,
  isShowModal, //是否显示modal
  reloadVersion,
  checkRows,
}) => {
  const directlyIssueModalRef: Ref<IOneModalRef> = useRef(null); //弹框的ref

  /* 关闭弹窗 */
  const onCloseModal = () => {
    if (directlyIssueModalRef) {
      if (directlyIssueModalRef.current) {
        if (directlyIssueModalRef.current.onModalCancel) {
          directlyIssueModalRef.current.onModalCancel();
        }
      }
    }
  };

  /**
   * 新增编辑回调
   * @param res 接口返回数据
   * @param callType true 为新增回调  || false 为编辑回调
   */
  const updateVersionCallback = (res: any, callType: boolean) => {
    if (res && res?.msgFlag) {
      onCloseModal();
      history.push({
        pathname: `/versionController/getVersionByID/{id}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          id: addVersionData?.id,
          delRouterName: history.location.pathname, //必传,要删除的路由名称
        },
      });
    } else {
      message.error(res?.msg);
    }
  };

  /**
   * 引入新增
   */
  const {
    addVersionRun,
    addVersionLoading,
    addVersionData,
  } = addVersionService(updateVersionCallback);

  /**直接签发回调 */
  const directlyIssueCallBack = (res: any) => {
    if (res && res?.msgFlag) {
      // message.success(res?.msg)
      onCloseModal();
      history?.push({
        pathname: `/versionController/getVersionByID/{id}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          id: checkRows?.id,
          delRouterName: history.location.pathname, //必传,要删除的路由名称
        },
      });
    } else {
      message.error(res?.msg);
    }
  };

  /**引入直接签发 */
  const { directlyIssueRun, directlyIssueLoading } = directlyIssueService(
    directlyIssueCallBack,
  );

  /**
   * 确认直接发布
   */
  const onOk = () => {
    let updatePayload: any = {};
    if (checkRows?.id) {
      /**checkState 审核状态(0：待审核 1：通过 2 驳回 3：作废) */
      /**state 系统版本的状态(0：起草 1：待审核 2：未通过 3：已通过 4：已作废) */
      updatePayload['msysVersion'] = {
        id: checkRows?.id,
        versionNum: checkRows?.versionNum,
      };
      // updatePayload['checkState'] = 1;
      updatePayload['type'] = '1';
      directlyIssueRun(updatePayload);
    } else {
      updatePayload = { ...checkRows };
      // updatePayload['checkState'] = 1;
      updatePayload['type'] = '1';
      addVersionRun(updatePayload);
    }
  };

  return (
    <>
      <OneModal
        open={true}
        maskClosable={true} //点击蒙层关闭弹窗 默认true
        getContainer={false}
        ref={directlyIssueModalRef}
        title={
          <div style={{ display: 'flex', alignItems: 'center' }}>
            <span
              style={{ marginRight: 10, fontSize: '24px', color: '#faad14' }}
            >
              <MyIcon type="icon-info-circle" />
            </span>
            <span>直接发布</span>
          </div>
        }
        width={document.body.clientWidth * 0.3}
        onCancel={onClose}
        bodyStyle={{
          padding: '15px 30px',
        }}
        centered
        onOk={onOk}
        confirmLoading={addVersionLoading || directlyIssueLoading}
      >
        <div style={{ fontSize: '18px', color: '#ff000d' }}>
          确认直接发布吗？
        </div>
      </OneModal>
    </>
  );
};
